/**
 * 
 */
package com.luckmgr.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Message;
import com.luckmgr.service.LuckRecordService;
import com.luckmgr.service.LuckService;
import com.luckmgr.service.MessageService;
import com.luckmgr.system.SystemData;
import com.luckmgr.vo.AskLuckVO;
import com.luckmgr.vo.LuckRecordVO;


/**
 * 
 * @author duanhx
 * @version [Ver 1.0; 2012-7-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("/luck")
public class LuckController {
	
	@Autowired
	LuckService luckServiceImpl;
	
	@Autowired
	LuckRecordService luckRecordService;
	
	@Autowired
	MessageService messageService;

	@RequestMapping(value="login",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String luckindex(Model model,HttpSession session,String acode,String uname) {
		AskLuckVO askLuckVO = new AskLuckVO();
		if(StringUtils.isNull(acode)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOACTIVITY);
			return JSON.toJSONString(askLuckVO);
		}
		if(StringUtils.isNull(uname)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOUSER);
			return JSON.toJSONString(askLuckVO);
		}
		
		if(!SystemData.ishaveActivity(acode)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOACTIVITY);
			return JSON.toJSONString(askLuckVO);
		}
		askLuckVO = luckServiceImpl.askAcivity(acode, uname);
		session.setAttribute(SystemConstant.ESSIONUSER, askLuckVO.getUnm());
		return JSON.toJSONString(askLuckVO);
	}
	
	@RequestMapping(value="",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String luck(Model model,HttpServletRequest request,HttpSession session,String acode) {
		String uname = (String)session.getAttribute(SystemConstant.ESSIONUSER);
		String ip = getIpAddr(request);
		AskLuckVO askLuckVO = new AskLuckVO();
		if(StringUtils.isNull(acode)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOACTIVITY);
			return JSON.toJSONString(askLuckVO);
		}
		if(StringUtils.isNull(uname)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOUSER);
			return JSON.toJSONString(askLuckVO);
		}
		
		if(!SystemData.ishaveActivity(acode)){
			askLuckVO.setAs(SystemConstant.LUCK_ASK_STATUS_NOACTIVITY);
			return JSON.toJSONString(askLuckVO);
		}
		askLuckVO = luckServiceImpl.luck(acode, uname,ip);
		session.setAttribute(SystemConstant.ESSIONUSER, askLuckVO.getUnm());
		return JSON.toJSONString(askLuckVO);
	}
	
	
	@RequestMapping(value="lotterylist",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String lotterylist(Model model,String acode) {
		if(StringUtils.isNull(acode)){
			return "";
		}
		return SystemData.findLottoRecord(acode);
	}
	
	@RequestMapping(value="luckRecord",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String luckRecord(Model model,String uname,String acode,@RequestParam(required = false)  Integer page,@RequestParam(required = false)  Integer currentpage) {
		if(StringUtils.isNull(uname) || StringUtils.isNull(acode)){
			return "";
		}
		if(null == page){
			page =1;
		}
		if(null == currentpage){
			currentpage =8;
		}
		PageBean<LuckRecordVO> pagebean = luckRecordService.selectPageBean(acode,uname, page, currentpage);
		if(pagebean!=null){
			return  JSON.toJSONString(pagebean);
		}
		return "";
	}
	
	
	@RequestMapping(value="message",produces="text/plain;charset=utf-8")
	@ResponseBody
	public String message(Model model,String acode) {
		if(StringUtils.isNull(acode)){
			return "";
		}
		Message message = messageService.selectBySeriesCode(acode);
		if(null == message)
			return "";
		return  message.getContent();
	}
	
	
	

	private String getIpAddr(HttpServletRequest request) { 
	       String ip = request.getHeader("x-forwarded-for"); 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getHeader("WL-Proxy-Client-IP"); 
	       } 
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
	           ip = request.getRemoteAddr(); 
	       } 
	       
	       if(!StringUtils.isNull(ip)) { 
	    	   if(ip.contains(",")){
	    		   String[] arrstr = ip.split(",");
	    		   if(null != arrstr && arrstr.length>0){
	    			   ip = arrstr[0];
	    		   }
	    	   }
	       }
	       return ip; 
	   } 
	
	
}
